<%@ page import="com.scalar.chiptrack.commons.Constants" %>
<%@ page import="com.scalar.chiptrack.jasper.base.JasperReportsHelper" %>
<%@ page import="com.scalar.chiptrack.utils.ConfigManager" %>
<%@ page import="net.sf.jasperreports.engine.JRExporterParameter" %>
<%@ page import="net.sf.jasperreports.engine.JasperPrint" %>
<%@ page import="net.sf.jasperreports.engine.export.JRHtmlExporter" %>
<%@ page import="net.sf.jasperreports.engine.export.JRHtmlExporterParameter" %>
<%@ page import="org.apache.struts.util.MessageResources" %>
<%@ page import="com.scalar.chiptrack.commons.Labels" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%String chipTracContext = "/" + ConfigManager.getChipTracContext(); %>
<%
    JRHtmlExporter exporter = new JRHtmlExporter();
    //JasperPrint jasperPrint = (JasperPrint) request.getAttribute(Constants.REQUEST_PARAM_JASPER_PRINT);
    String printSessionName = JasperReportsHelper.getJasperPrintSessionParamName(request, null);
    JasperPrint jasperPrint = (JasperPrint)request.getSession().getAttribute(printSessionName);
    //request.getSession().setAttribute(printSessionName, jasperPrint);

    int pageIndex = 0;
    int lastPageIndex = 0;
    if (jasperPrint.getPages() != null) {
        lastPageIndex = jasperPrint.getPages().size() - 1;
    }

    String pageStr = request.getParameter("page");
    try {
        pageIndex = Integer.parseInt(pageStr);
    }
    catch (Exception e) {
    }

    if (pageIndex < 0) {
        pageIndex = 0;
    }

    if (pageIndex > lastPageIndex) {
        pageIndex = lastPageIndex;
    }

%>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
    <title>Scalar Soft - ChipTrac</title>
    <style type="text/css">
        .buttonPagination{
            font-size:8pt;
            font-family:verdana, arial, helvetica;
            font-weight:bold;
            color:#000000;
            border: 1px solid #000;
            cursor: hand;
            background: #d1cccc;
            height:22px;
        }
        .valuetxfield {
            font-size:8pt;
            font-family:verdana, arial, helvetica;
            font-weight:bold;
            color:#000000;
            border-style:solid;
            border-color:#000000;
            border-width:1px;
            background-color:#FFFFee;
            text-align: right;
        }

        .labelField {
            font-size:8pt;
            font-family:verdana, arial, helvetica;
            
            color:#000000;
        }
    </style>
    <script type="text/javascript">
        function loadReport(withPagination) {
            var url = '<%=request.getAttribute(Constants.REQUEST_PARAM_HTML_URI)%>&<%=Constants.REQUEST_PARAM_USE_CACHE%>=true&<%=Constants.REQUEST_PARAM_IGNORE_PAGINATION%>='+!withPagination;
            window.location.href = url;
        }
    </script>
</head>
<body text="#000000" link="#000000" alink="#000000" vlink="#000000">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td width="50%">&nbsp;</td>
        <td align="left">
            <hr size="0" color="#000000" style="margin-top: 0px; margin-bottom: 1px;">
            <table width="100%" cellpadding="0" cellspacing="0" border="0ne" style="margin-top: 0px; margin-bottom: 0px;">
                <tr >
                    <td >
                        <a href="<%=request.getAttribute(Constants.REQUEST_PARAM_HTML_URI)%>"><img src="<%=chipTracContext%>/jsp/includes/images/reload.GIF" border="0" style="vertical-align: middle;" title="Reload"></a>
                    </td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="6"/></td>
                    <%
                        if (pageIndex > 0) {
                    %>
                    <td >
                        <a href="<%=request.getAttribute(Constants.REQUEST_PARAM_HTML_URI)%>&page=0&<%=Constants.REQUEST_PARAM_USE_CACHE%>=true"><img src="<%=chipTracContext%>/jsp/includes/images/first.GIF" border="0" style="vertical-align: middle;" title="First page"></a>
                    </td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="2"/></td>
                    <td >
                        <a href="<%=request.getAttribute(Constants.REQUEST_PARAM_HTML_URI)%>&page=<%=pageIndex - 1%>&<%=Constants.REQUEST_PARAM_USE_CACHE%>=true"><img src="<%=chipTracContext%>/jsp/includes/images/previous.GIF" border="0" style="vertical-align: middle;" title="Previous page"></a>
                    </td>
                    <%
                    } else {
                    %>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/first_grey.GIF" border="0" style="vertical-align: middle;"></td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="2"/></td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/previous_grey.GIF" border="0" style="vertical-align: middle;"></td>
                    <%
                        }
                    %>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="4"/></td>
                    <td  class="labelField">Page</td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="4"/></td>
                    <td ><input type="text" class="valuetxfield" value="<%=pageIndex+1%>" size="4"/></td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="4"/></td>
                    <td  class="labelField">of</td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="4"/></td>
                    <td  class="labelField" style="font-weight: bold;"><%=lastPageIndex+1%></td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="4"/></td>
                    <%

                        if (pageIndex < lastPageIndex) {
                    %>
                    <td >
                        <a href="<%=request.getAttribute(Constants.REQUEST_PARAM_HTML_URI)%>&page=<%=pageIndex + 1%>&<%=Constants.REQUEST_PARAM_USE_CACHE%>=true"><img src="<%=chipTracContext%>/jsp/includes/images/next_page.GIF" border="0" style="vertical-align: middle;" title="Next page"></a>
                    </td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="2"/></td>
                    <td >
                        <a href="<%=request.getAttribute(Constants.REQUEST_PARAM_HTML_URI)%>&page=<%=lastPageIndex%>&<%=Constants.REQUEST_PARAM_USE_CACHE%>=true"><img src="<%=chipTracContext%>/jsp/includes/images/last.GIF" border="0" style="vertical-align: middle;" title="Last page" ></a>
                    </td>
                    <%
                    } else {
                    %>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/next_grey.GIF" border="0" style="vertical-align: middle;"></td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="2"/></td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/last_grey.GIF" border="0" style="vertical-align: middle;"></td>
                    <%
                        }
                    %>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="4"/></td>
                    <td>
                        <%
                            String paginationBtnLabel = null;
                            String paginationBtnTooltip = null;
                            if("true".equalsIgnoreCase(request.getParameter(Constants.REQUEST_PARAM_IGNORE_PAGINATION))) {
                                paginationBtnLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.report.withPagination" );
                                paginationBtnTooltip = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.report.withPagination.tooltip" );
                            } else {
                                paginationBtnLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.report.noPagination" );
                                paginationBtnTooltip = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.report.noPagination.tooltip" );
                            }
                        %>
                        <input class="buttonPagination" type="button"
                            value="<%=paginationBtnLabel%>"
                            title="<%=paginationBtnTooltip%>"
                            onclick="loadReport(<%="true".equalsIgnoreCase(request.getParameter(Constants.REQUEST_PARAM_IGNORE_PAGINATION))%>)">
                    </td>
                    <td  width="100%">&nbsp;</td>
                    <td ><a href="<%=request.getAttribute(Constants.REQUEST_PARAM_PDF_URI)%>"><img src="<%=chipTracContext%>/jsp/includes/images/AcrobatDocument_24x24.gif" border="0" title="Download PDF format"></a></td>
                    <td ><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width="3"/></td>
                    <td ><a href="<%=request.getAttribute(Constants.REQUEST_PARAM_EXCEL_URI)%>"><img src="<%=chipTracContext%>/jsp/includes/images/ExcelDocument_24x24.gif" border="0" title="Download Excel format"></a></td>
                </tr>
            </table>
            <hr size="1" color="#000000" style="margin-top: 1px; margin-bottom: 0px;">
        </td>
        <td width="50%" valign="top" align="right" class="labelField">[<a href="<%=request.getAttribute(Constants.REQUEST_PARAM_REFERER)%>">Back</a>]</td>
    </tr>
    <tr>
        <td width="50%">&nbsp;</td>
        <td align="center">
            <%--<%=sbuffer.toString()%>--%>
            <%
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                //exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer);
                exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + "/jasper/image?" + "jrprint=" + printSessionName + "&image=");
                if(pageIndex >= 0)
                    exporter.setParameter(JRExporterParameter.PAGE_INDEX, Integer.valueOf(pageIndex));
                exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
                exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
                exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");
                exporter.setParameter(JRHtmlExporterParameter.IS_WRAP_BREAK_WORD, Boolean.TRUE);
                exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT, JRHtmlExporterParameter.SIZE_UNIT_POINT);
                exporter.setParameter(JRHtmlExporterParameter.FLUSH_OUTPUT, Boolean.TRUE);
                //exporter.setParameter(JRHtmlExporterParameter.ZOOM_RATIO, new Float(1.3));

                exporter.exportReport();
            %>
        </td>
        <td width="50%">&nbsp;</td>
    </tr>
</table>
</body>
</html>